package net.ib.mtalk.network.task;

import java.io.UnsupportedEncodingException;

import com.google.protobuf.ByteString;

import net.ib.mntalk.protocol.Common;
import net.ib.mntalk.protocol.Common.AccountInfo;
import net.ib.mntalk.protocol.Common.AccountToken;
import net.ib.mntalk.protocol.Common.PhoneAccountInfo;
import net.ib.mntalk.protocol.Common.RequestToken;
import net.ib.mntalk.protocol.Session.AcquireAccountAuthTokenReq;
import net.ib.mntalk.protocol.Session.AcquireAccountAuthTokenRes;
import net.ib.mtalk.constant.HandlerMsgConst;
import net.ib.mtalk.global.GlobalPreferences;
import net.ib.mtalk.network.TcpEnvelope;
import net.ib.mtalk.network.MethodId.Session;
import net.ib.mtalk.util.MTalkLogWriter;
import net.ib.mtalk.util.SmsMessager;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

public class AcquireAccountAuthTask extends AsyncNetTask/* implements Receiver*/{
	
	AccountInfo accountInfo = null;
	AccountToken mAccountToken;
	RequestToken mRequestToken;
	private AcquireAccountAuthTokenReq mAcquireAccountAuthTokenReq;
	String appSecret = "abcdefghijklmnopqrstuvwxyz";
	
	public AcquireAccountAuthTask(Context context, Handler handler, AcquireAccountAuthTokenReq acquireAccountAuthTokenReq) {
		super(context, handler);
		this.mAcquireAccountAuthTokenReq = acquireAccountAuthTokenReq;
		//nrr = new NetResultReceiver(new Handler(), this);
	}

	@Override
	protected TcpEnvelope onPreExecute() {
		TcpEnvelope revelop = new TcpEnvelope.Builder()
		.setType(TcpEnvelope.REQUEST)
		.setMessage(mAcquireAccountAuthTokenReq)
		.setMethodId(Session.Registration.AcquireAccountAuthToken)
		.build();
		
		return revelop;
	}

	@Override
	protected void onPostExecute(int resultCode, Object sentObject, Object receivedObject) {
		MTalkLogWriter.v("AcquireAccountAuthTask.onPostExecute() - STT");
		if (receivedObject == null) {
			mUiHandler.sendMessageAtFrontOfQueue(mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_UNKNOWN_ERROR));
			return;
		}
		
		TcpEnvelope resultEnvelope = (TcpEnvelope) receivedObject;
		AcquireAccountAuthTokenRes res = (AcquireAccountAuthTokenRes) resultEnvelope.getMessage();
		
		if(res.getResponseCode() == Common.SUCCESS){
			Message msg = mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_ACQUIRE_ACCOUNT_AUTH_SUCCESS);
			msg.obj = res;
			mUiHandler.sendMessageAtFrontOfQueue(msg);
		} else{
			mUiHandler.sendMessageAtFrontOfQueue(mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_ACQUIRE_ACCOUNT_AUTH_FAIL));
		}
	}
}
